package com.kelvin.paopao.module.login.register.registerStep2;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.injector.components.DaggerRegisterStep2Component;
import com.kelvin.paopao.injector.modules.RegisterStep2Module;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.login.register.updateInfo.UpdateInfoActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/7.
 */

public class RegisterStep2Activity extends BaseActivity<RegisterStep2Presenter> implements IRegisterStep2View {

    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.tv_phone_number)
    TextView tvPhoneNumber;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_retry)
    TextView tvRetry;
    @BindView(R.id.tv_complete)
    TextView tvComplete;

    private String phone;
    private String password;
    private String wechat_id;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_register_step2;
    }

    @Override
    protected void initInjector() {
        DaggerRegisterStep2Component.builder().applicationComponent(getAppComponent()).
                registerStep2Module(new RegisterStep2Module(this)).build().inject(this);
    }

    @Override
    protected void initViews() {
        customToolbar.setTitle("提交验证码");
        customToolbar.showBackIcon(this);

        phone = getIntent().getStringExtra("phone");
        password = getIntent().getStringExtra("password");
        wechat_id = getIntent().getStringExtra("wechat_id");
        tvPhoneNumber.setText(phone);

        etYzm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String yzm = etYzm.getText().toString().trim();
                if (!TextUtils.isEmpty(yzm)) {
                    tvComplete.setBackgroundResource(R.drawable.shape_login_btn);
                    tvComplete.setTextColor(ContextCompat.getColor(RegisterStep2Activity.this, android.R.color.white));
                } else {
                    tvComplete.setBackgroundResource(R.drawable.shape_login2_btn);
                    tvComplete.setTextColor(ContextCompat.getColor(RegisterStep2Activity.this, R.color.text_color_dark));
                }
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.tv_retry, R.id.tv_complete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_retry:
                mPresenter.getValidation(phone);
                break;
            case R.id.tv_complete:
                String code = etYzm.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showToast(this, "请填写验证码~~");
                    return;
                }
                UpdateInfoActivity.launch(this, phone, password, code, wechat_id);
                break;
        }
    }

    public static void launch(Context context, String phone, String password, String wechat_id) {
        Intent intent = new Intent(context, RegisterStep2Activity.class);
        intent.putExtra("phone", phone);
        intent.putExtra("password", password);
        intent.putExtra("wechat_id", wechat_id);
        context.startActivity(intent);
    }

    @Override
    public void updateTextView(int count) {
        tvRetry.setText(count + "秒后重试");

        if (count == 0) {
            tvRetry.setEnabled(true);
            tvRetry.setText("获取验证码");
        } else {
            tvRetry.setEnabled(false);
        }
    }

    @Override
    public void enableRetry(boolean isEnable) {
        tvRetry.setEnabled(isEnable);
    }


}
